const { ipcMain } = require("electron");
const { createWin } = require("./share");
const path = require("path");

const createPreviewWin = (parentWin) => {
    const previewWin = createWin({
        width: 660,
        height: 444,
        modal: true,
        parent: parentWin,
        webPreferences: {
            nodeIntegration: true, // 允许预加载js文件使用require模块
            preload: path.join(__dirname, "../src/view/preview/preload.js"),
        },
    }, "./src/view/preview/index.html");
    return previewWin;
};
const createIpcPre = (parentWin)=> {
    let prevWin
    ipcMain.on("open-preview", (e, arg) => {
        prevWin = createPreviewWin(parentWin);
        // console.log(arg)

        prevWin.webContents.on('did-finish-load', ()=> {
            prevWin.webContents.send("preview-data", arg);
        })
        // 监听窗口关闭
        prevWin.on('closed', ()=> {
            prevWin = null
        })
    });
    
    ipcMain.on("close-preview", () => {
        prevWin.close();
    });
}
module.exports = {
    createIpcPre
}